﻿// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include<iostream>
#include<string> 
#include<ctime>
#include<cstdlib>
#include<vector>
#include<fstream>
#include<sstream>


using namespace std;


class User {
public:
	bool Register(string oID, string oName, string oPassword);
	int Login(string iID, string iPassword, string& name);
	bool IsRepetition(string oID);
	void Loading();

//private:
	vector<string> ID;
	vector<string> Name;
	vector<string> Password;

};

bool User::IsRepetition(string oID)
{
	for (int i = 0; i < ID.size(); i++)
	{
		if (ID[i] == oID)
			return true;
	}
	return false;
}

bool User::Register(string oID, string oName, string oPassword)
{
	ofstream outfile("User_in.txt", ios::app);
	if (!outfile)
	{
		cout << "请检查信息文件" << endl;
		return false;
	}

	outfile << oID << " " << oName << " " << oPassword << endl;

	ID.push_back(oID);
	Name.push_back(oName);
	Password.push_back(oPassword);
	outfile.close();
	cout << "注册成功，正在跳转，请稍等" << endl;
	cout << "您的注册信息为：" << endl;
	cout << "ID:" << oID << " " << "姓名:" << oName << " " << "密码:" << oPassword << endl;
	return true;
}


int User::Login(string iID, string iPassword, string& name) {
	for (int i = 0; i < ID.size(); i++) {
		if (iID == ID[i]) {
			cout << "ID正确！" << endl;
			//cout << Password[i]<<sizeof(Password[i]) << endl;
			if (iPassword == Password[i]) {
				cout << "密码正确!" << endl;
				cout << "******登陆成功！请稍后******" << endl;
				cout << "您的个人信息为：" << endl;
				cout << "ID:" << iID <<endl<< "姓名:" << Name[i] << endl;
				cout << "****************************" << endl;
				name = Name[i];
				return 0;
			}
		}
	}
	cout << "您输入的账号或密码有误！请重新输入！" << endl;
	return 1;
}

int findspace(const string str,int x=2) {
	int count1 = 0,count2=0;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] != ' ') count1++;
		if (str[i] == ' ') count2++;
		if (count2 == x) return (count1+count2-1);
	}
	return 0;
}

void User::Loading() {
	ifstream infile("User_in.txt");
	if (!infile) {
		cout << "请检查信息文件！" << endl;
	}
	string line;
	while (getline(infile, line)) {
		string id, name, password;
		size_t pos = 0;
		//提取&加载ID信息 
		pos = line.find_first_of(' ');
		id = line.substr(0, pos);
		ID.push_back(id);
		//提取&加载姓名信息 
		name = line.substr(id.size() + 1);
		pos = name.find_first_of(' ');
		name = name.substr(0, pos);
		Name.push_back(name);
		//提取&加载密码信息 
		password = line.substr(id.size() + name.size() + 2);
		//cout << password << endl;
		Password.push_back(password);

		/*stringstream ss(line);
		getline(ss,id,' ');
		getline(ss,name,' ');
		getline(ss,password,';');
		ID.push_back(id);
		Name.push_back(name);
		Password.push_back(password);*/
	}
	infile.close();
}

bool pick_bottle(string& Rc,string name) {
	ifstream Readfile("漂流瓶.txt", ios::in);
	string Re;
	int random;
	if (!Readfile)//文件打开失败:返回0
	{
		cout << "无法打开文件!请检查或创建!" << endl;
		return false;
	}
	vector<string> lines;
	string line;
	while (getline(Readfile, line)) {
		lines.push_back(line);
	}
	Readfile.close();
	
	if (lines.empty()) {
		cout << "暂无漂流瓶！" << endl;
		return false;
	}
	
	else {
			int i, k = 0;
			string str0;
			for (i = 0; i < lines.size(); i++) {
				size_t x = 0;
				str0 = lines[i];
				x = lines[i].find_first_of(" ");
				str0 = str0.substr(x + 8);
				if (str0 != name) k++;
			}
			if (k == 0) {
				cout << "暂无漂流瓶！" << endl;
				return false;
			}
		}
		size_t size = lines.size();
		srand((unsigned int)time(NULL));
		do {
			size_t p = 0;
			random = rand() % (size);
			Re = lines[random];
			p = Re.find_first_of(" ");
			Re = Re.substr(p + 8);
			//cout << lines[random] << endl;

		} while (Re == name);
		if (Re != name) {
			cout << "捡到一个漂流瓶：" << endl;
			string str = lines[random], begin;
			size_t pos = 0;
			const string basis = "Hide";
			pos = str.find_first_of(' ');
			begin = str.substr(0, 4);
			//cout << cname << endl;
			if (begin == basis) {
				//cout << pos;
				string str1 = str.substr(5, pos - 5);
				//cout << str1<<endl;
				string str2 = str1 + " 发送者:匿名";
				cout << str2 << endl;
				str = str.substr(pos + 8);
				Rc = "Hide" + str;
				//	cout << Rc<<endl;
			}
			else {
				str = str.substr(pos + 8);
				cout << lines[random] << endl;
				Rc = str;
			}
		}
		//cout << str << endl;
	
	return true;
}

bool throw_bottle(string name) {
	ofstream wout("漂流瓶.txt", ios::app);
	if (!wout)
	{
		cout << "请检查信息文件" << endl;
		return false;
	}
	cout << "你想匿名发送吗?" << endl;
	char op;
	cin >> op;
	string words;
	cout << "请写下你想说的话：" << endl;
	cin >> words;
	if (op == 'y' || op == 'Y') {

		wout << "Hide:" << '"' << words << '"' << " " << "发送者:" << name << endl;
	}
	else {
		wout << '"' << words << '"' << " " << "发送者:" << name << endl;
	}
	cout << "您已成功扔出一个漂流瓶！" << endl;
	wout.close();
}

bool Reply_message(string name, string Rc) {
	ofstream refile("Reply_message.txt", ios::app);
	if (!refile) {
		cout << "请检查信息文件" << endl;
		return false;
	}
	cout << "请写下你想回复的消息:" << endl;
	string message;
	cin >> message;
	refile << '"' << message << '"' << " " << "回复人:" << name << " " << "回复对象:" << Rc << endl;
	refile.close();
	cout << "您已成功回复!" << endl;
}

bool show_My_Reply_message(string name) {
	ifstream infile("Reply_message.txt");
	if (!infile) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname, hname;
	int i = 0;
	while (getline(infile, line)) {
		size_t pos = 0, pos1 = 0;
		pos = line.find_first_of(' ');
		cname = line.substr(pos + 8);
		pos = cname.find_first_of(' ');
		cname = cname.substr(0, pos);
		pos1 = findspace(line);
		hname = line.substr(pos1 + 10);
		//cout << cname << endl;
		//cout << hname<<endl;
		if (cname == name) {
			i++;
			cout << "我的第" << i << "条回复:" << endl;
			if ((sizeof(hname)) > 4) {
				string tname = hname.substr(0, 4);
				if (tname == "Hide") {
					size_t pos2 = 0;
					pos2 = line.find_first_of(" ");
					string str1 = line.substr(0, pos2 + 1);
					//cout << str1<<endl;
					string str2 = str1 + "回复对象:匿名";
					cout << str2 << endl;
				}
				else {
					size_t pos3 = 0,pos4 =0;
					pos3 = line.find_first_of(" ");
					pos4 = findspace(line);
					string str = line.substr(0, pos3+1)+line.substr(pos4+1);
					cout << str<< endl;
				}
			}
		}
	}
	infile.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未发出回复!" << endl;
		return true;
	}
}

bool show_My_bottle(string name) {
	ifstream inbottle("漂流瓶.txt");
	if (!inbottle) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname;
	int i = 0;
	while (getline(inbottle, line)) {
		size_t pos = 0;
		pos = line.find_first_of(' ');
		cname = line.substr(pos + 8);
		//cout << cname<<endl;
		if (cname == name) {
			i++;
			cout << "您扔出的第" << i << "个漂流瓶为:" << endl;
			cout << line << endl;
		}
	}
	inbottle.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未发出漂流瓶!" << endl;
		return true;
	}
}

bool show_Reply_to_me(string name) {
	ifstream readin("Reply_message.txt");
	if (!readin) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname;
	int i = 0;
	while (getline(readin, line)) {
		size_t pos = 0;
		pos =findspace(line);
		cname =line.substr(pos + 10);
		if ((sizeof(cname)) > 4) {
			string tname = cname.substr(0, 4);
			if (tname == "Hide") {
				cname = cname.substr(4);
			}
		}
		//cout << cname << endl;
		if (cname == name) {
			i++;
			cout << "我收到的第" << i << "条回复:" << endl;
			cout << line.substr(0, findspace(line))  << endl;
		}

	}
	readin.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未收到回复!" << endl;
		return true;
	}

}



int main() {
	int x;
	User user;
	user.Loading();
	string id, name, password;
	cout << "****欢迎进入漂流瓶系统!****" << endl;
	cout << "请选择您想进行的操作:"<<endl<<"1.注册"<<endl<<"2.登录"<<endl<<"其他:退出" << endl;
	cout << "***************************"<<endl;
	cin >> x;
	if (x == 1) {
		cout << "请输入您要注册的ID（可使用字母和阿拉伯数字）：" << endl;
		cin >> id;
		while ((user.IsRepetition(id)) == true)
		{
			cout << "对不起，您注册的ID已存在,请重新注册!" << endl;
			cout << "请输入您要注册的ID（可使用字母和阿拉伯数字）：" << endl;
			cin >> id;
		}
		cout << "请输入您喜欢的昵称：" << endl;
		cin >> name;
		cout << "请设置您的密码：" << endl;
		cin >> password;
		user.Register(id, name, password);
	}
	else if (x == 2) {
		int check;
		do {
			cout << "请输入您的ID:" << endl;
			cin >> id;
			cin.sync();
			cout << "请输入您的密码:" << endl;
			cin >> password;
			cin.sync();
			check = user.Login(id, password, name);
		} while (check == 1);
	}
	else {
		cout << "感谢使用，再见！" << endl;
		exit(0);
	}


	char op;
	do {
		cout << "****************************" << endl;
		cout << "请选择您想进行的操作:" << endl;
		cout << "t:扔漂流瓶" << endl << "p:捡漂流瓶" << endl;
		cout<<"3.查看已发出的漂流瓶(Hide表示匿名扔出)"<<endl<<"4.查看我发出的回复"<<endl<<"5.查看我收到的回复"<<endl<<"0:退出程序"<< endl;
		cout << "****************************" << endl;
		cin >> op;
		if (op == 't') {
			throw_bottle(name);
		}
		else if (op == 'p') {
			char op2;
			string Rc;
			if ((pick_bottle(Rc, name)) == true) {
				cout << "你想回复这条漂流瓶吗？" << endl;
					cin >> op2;
					if (op2 == 'y' || op == 'Y') {

						Reply_message(name, Rc);
					}
					else cout << "你没有选择回复!" << endl;
			}
		}
		else if (op == '3') {
			show_My_bottle(name);
		}
		else if (op == '4') {
			show_My_Reply_message(name);
		}
		else if (op == '5') {
			show_Reply_to_me(name);
		}
	} while (op != '0');
	return 0;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
